package com.ruoyi.report_lq.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_lq.domain.bo.ReportRhlqCommonBo;
import com.ruoyi.report_lq.domain.vo.ReportRhlqCommonVo;
import com.ruoyi.report_lq.domain.ReportRhlqCommon;
import com.ruoyi.report_lq.mapper.ReportRhlqCommonMapper;
import com.ruoyi.report_lq.service.IReportRhlqCommonService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author zyb
 * @date 2024-06-14
 */
@RequiredArgsConstructor
@Service
public class ReportRhlqCommonServiceImpl implements IReportRhlqCommonService {

    private final ReportRhlqCommonMapper baseMapper;

    /**
     * 查询【请填写功能名称】
     */
    @Override
    public ReportRhlqCommonVo queryById(Long commonId){
        return baseMapper.selectVoById(commonId);
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @Override
    public TableDataInfo<ReportRhlqCommonVo> queryPageList(ReportRhlqCommonBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ReportRhlqCommon> lqw = buildQueryWrapper(bo);
        Page<ReportRhlqCommonVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @Override
    public List<ReportRhlqCommonVo> queryList(ReportRhlqCommonBo bo) {
        LambdaQueryWrapper<ReportRhlqCommon> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ReportRhlqCommon> buildQueryWrapper(ReportRhlqCommonBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ReportRhlqCommon> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getRhlqjlId() != null, ReportRhlqCommon::getRhlqjlId, bo.getRhlqjlId());
        lqw.eq(bo.getRhlqjlType() != null, ReportRhlqCommon::getRhlqjlType, bo.getRhlqjlType());
        lqw.eq(StringUtils.isNotBlank(bo.getJizlqcd()), ReportRhlqCommon::getJizlqcd, bo.getJizlqcd());
        lqw.eq(StringUtils.isNotBlank(bo.getRuhlqdh()), ReportRhlqCommon::getRuhlqdh, bo.getRuhlqdh());
        lqw.eq(StringUtils.isNotBlank(bo.getQuydd()), ReportRhlqCommon::getQuydd, bo.getQuydd());
        lqw.eq(StringUtils.isNotBlank(bo.getRuhlqpz()), ReportRhlqCommon::getRuhlqpz, bo.getRuhlqpz());
        lqw.eq(StringUtils.isNotBlank(bo.getJizlqzl()), ReportRhlqCommon::getJizlqzl, bo.getJizlqzl());
        lqw.eq(StringUtils.isNotBlank(bo.getJizlqbh()), ReportRhlqCommon::getJizlqbh, bo.getJizlqbh());
        lqw.eq(StringUtils.isNotBlank(bo.getRuhlqpzjdh()), ReportRhlqCommon::getRuhlqpzjdh, bo.getRuhlqpzjdh());
        lqw.eq(StringUtils.isNotBlank(bo.getJizlqzljbh()), ReportRhlqCommon::getJizlqzljbh, bo.getJizlqzljbh());
        lqw.eq(StringUtils.isNotBlank(bo.getJilcdjzl()), ReportRhlqCommon::getJilcdjzl, bo.getJilcdjzl());
        lqw.eq(StringUtils.isNotBlank(bo.getShiyff()), ReportRhlqCommon::getShiyff, bo.getShiyff());
        lqw.eq(StringUtils.isNotBlank(bo.getFujsm()), ReportRhlqCommon::getFujsm, bo.getFujsm());
        return lqw;
    }

    /**
     * 新增【请填写功能名称】
     */
    @Override
    public Boolean insertByBo(ReportRhlqCommonBo bo) {
        ReportRhlqCommon add = BeanUtil.toBean(bo, ReportRhlqCommon.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setCommonId(add.getCommonId());
        }
        return flag;
    }

    /**
     * 修改【请填写功能名称】
     */
    @Override
    public Boolean updateByBo(ReportRhlqCommonBo bo) {
        ReportRhlqCommon update = BeanUtil.toBean(bo, ReportRhlqCommon.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ReportRhlqCommon entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除【请填写功能名称】
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
